<?php

function is_table_with_no_fk($conn, $table) {
  $result_fk = $conn->cubrid_schema(PDO::CUBRID_SCH_IMPORTED_KEYS, $table);
  if($result_fk != null && count($result_fk)) {
    return false;
  } else {
    return true;
  }
}
  
function tables_are_processed($referenced_tables, $processed_tables) {
  $ret = true;
  
  foreach ($referenced_tables as $referenced_table) {
    if(!in_array($referenced_table, $processed_tables)) {
      $ret = false;
      break;
    }
  }
  
  return $ret;
}
  
function get_table_fk($conn, $table) {
  $fk = array();
  
  $result_fk = $conn->cubrid_schema(PDO::CUBRID_SCH_IMPORTED_KEYS, $table);
  if($result_fk != null && count($result_fk) >0) {
    foreach ($result_fk as $row_fk ) {
      $fk[] = $row_fk["PKTABLE_NAME"];
    }
  }
  
  return $fk;
}
  
function column_is_pk($conn, $table, $column) {
  $str = "";
  $sql = "";
  
  $sql .= "select count(a.index_name)"; 
  $sql .= " from db_index_key a, db_index b";
  $sql .= " where a.index_name = b.index_name";
  $sql .= " and b.is_primary_key='YES'";
  $sql .= " and a.class_name='".$table."'";
  $sql .= " and a.key_attr_name='".$column."'";
  
  $result = $conn->query($sql);
  if($result != null) {
    foreach ($result as $row) {
      $str = $row[0];
      break;
    }
  }

  if($str == "1") {
    return "Yes";
  } else {
    return "";
  }
}
  
function sort_tables_by_fk($conn, $tables) {
  $processed = array();
  //$not_processed = split(",", $tables);
  $not_processed = explode(",", $tables);

  while(count($not_processed)>0) {
    foreach ($not_processed as $idx => $t) {
      if(is_table_with_no_fk($conn, $t)) {
        $processed[] = $t;
        unset($not_processed[$idx]);
      } else {
        if(tables_are_processed(get_table_fk($conn, $t), $processed)) {
          $processed[] = $t;
          unset($not_processed[$idx]);
        }
      }
    }
  }  
  
  $ret = "";
  foreach ($processed as $t) {
    $ret .= $t;
    $ret .= ",";
  }
  if($ret != "") {
    $ret = substr($ret, 0, strlen($ret)-1);
  }
  
  return $ret;  
}

function format_file_size($size) {
  $sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
  if ($size == 0) { 
    return('n/a'); 
  } else {
    return (round($size/pow(1024, ($i = floor(log($size, 1024)))), $i > 1 ? 2 : 0) . $sizes[$i]); 
  }
}  

function exportCSV($conn, $table, $filename) {
  $csv_terminated = PHP_EOL;
  $csv_separator = ",";
  $csv_enclosed = '"';
  $csv_escaped = "\\";
  $sql_meta = "select attr_name from db_attribute where class_name = '".$table."' order by def_order asc";
  $sql = "select * from ".$table;
 
  $fh = fopen($filename, 'w');

  $result = $conn->query($sql_meta);
  if($result != null) {
    $fields_cnt = 0;
    $schema_insert = '';
 
    while($col = $result->fetch()) {
      $l = $csv_enclosed.$col['attr_name'].$csv_enclosed;
      $schema_insert .= $l;
      $schema_insert .= $csv_separator;
      $fields_cnt++;
    }
  }
  
  $schema_insert = substr($schema_insert, 0, strlen($schema_insert)-1).$csv_terminated;
  fwrite($fh, $schema_insert);

  $result = $conn->query($sql);
  if($result != null) {
    while ($row = $result->fetch()) {
      $schema_insert = '';
      
      for ($j = 0; $j < $fields_cnt; $j++) {
        if ($row[$j] == '0' || $row[$j] != '') {
          if ($csv_enclosed == '') {
            $schema_insert .= $row[$j];
          } else {
            $schema_insert .= $csv_enclosed.$row[$j].$csv_enclosed;
          }
        } else {
          $schema_insert .= '';
        }
 
        if ($j < $fields_cnt - 1) {
          $schema_insert .= $csv_separator;
        }
      }
      
      $out .= $schema_insert;
      $out .= $csv_terminated;
      
      fwrite($fh, $out);
      $out = "";
    }
  }
  
  fwrite($fh, $out);
  fclose($fh);
}

?>
